﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using SchoolDB.Util;

/// <summary>
///DataProvider 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
[System.Web.Script.Services.ScriptService]
public class DataProvider : System.Web.Services.WebService
{

    public DataProvider()
    {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    //[WebMethod]
    //public string HelloWorld()
    //{
    //    return "Hello World";
    //}



    [WebMethod]
    public string GetCourseTypeByTeacher(string tid)
    {
        try
        {
            SDBManage ByT = new SDBManage();
            int i;
            DataTable TeacherData = ByT.GetCourseTypeByTeacher(tid);
            DataRowCollection Rows = TeacherData.Rows;

            IList<CourseByTeacherInfo> list = new List<CourseByTeacherInfo>();
            for (i = 0; i < TeacherData.Rows.Count; i++)
            {
                CourseByTeacherInfo temp = new CourseByTeacherInfo();
                DataRow currRow = Rows[i];
                //temp.TID = (string)currRow["TID"];
                //temp.SID = (string)currRow["SID"];
                //temp.SName = (string)currRow["SName"];
                temp.Name = (string)currRow["CourseName"];
                temp.Id = (string)currRow["CourseID"];
                list.Add(temp);
            }

            //Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();
            return JsonHelper.Serialize(list);
        }
        catch (Exception e)
        {
            //return "{}";
            return e.Message;
        }
    }


    [WebMethod]
    public string GetCourseByTeacher(string tid)                         //返回教师的课程信息
    {
        try 
        {
            SDBManage ByT = new SDBManage();
            int i;
            DataTable TeacherData = ByT.GetCourseByTeacher(tid);
            DataRowCollection Rows = TeacherData.Rows;

            IList<CourseByTeacherInfo> list = new List<CourseByTeacherInfo>();
            for (i = 0; i < TeacherData.Rows.Count; i++)
            {
                CourseByTeacherInfo temp = new CourseByTeacherInfo();
                DataRow currRow = Rows[i];
                //temp.TID = (string)currRow["TID"];
                temp.CourseTypeID = (string)currRow["CourseID"];
                temp.CourseTypeName = (string)currRow["CourseName"];
                temp.Name = (string)currRow["ClassName"];
                temp.Id = (string)currRow["ClassID"];
                list.Add(temp);
            }

            //Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();
            return JsonHelper.Serialize(list);
        }
        catch(Exception e)
        {
            //return "{}";
            return e.Message;
        }
    }
    
    [WebMethod]
    public string GetCourseByStudent(string sid)                         //返回学生的课程信息
    {
        try
        {
            SDBManage ByS = new SDBManage();
            int i;
            DataTable StudentData = ByS.GetCourseByStudent(sid);
            DataRowCollection Rows = StudentData.Rows;
            IList<CourseByStudentInfo> list = new List<CourseByStudentInfo>();

            for (i = 0; i < StudentData.Rows.Count; i++)
            {
                CourseByStudentInfo temp = new CourseByStudentInfo();
                DataRow currRow = Rows[i];
                //temp.SID = (string)currRow["SID"];
                temp.TID = (string)currRow["TID"];
                temp.TName = (string)currRow["TName"];
                temp.Name = (string)currRow["ClassName"];
                temp.Id = (string)currRow["ClassID"];
                list.Add(temp);
            }
            return JsonHelper.Serialize(list);
        }
        catch(Exception e)
        {

            //return "{}";
            return e.Message;
        }
    }


    [WebMethod]
    public String GetStudentByCourse(string courseID)
    {
        try
        {
            SDBManage ByS = new SDBManage();
            int i;
            DataTable StudentData = ByS.GetStudentByCourse(courseID);
            DataRowCollection Rows = StudentData.Rows;
            IList<UserInfo> list = new List<UserInfo>();
            

            for (i = 0; i < StudentData.Rows.Count; i++)
            {
                UserInfo temp = new UserInfo();
                DataRow currRow = Rows[i];
                temp.ID = (string)currRow["ID"];
                temp.Name = (string)currRow["Name"];
                temp.Sex = (string)currRow["Sex"];
                temp.Institute = (string)currRow["Institute"];
                temp.Department = (string)currRow["Department"];
                temp.Class = (string)currRow["Class"];
                list.Add(temp);
            }
            return JsonHelper.Serialize(list);
        }
        catch(Exception e)
        {
            //return "{}";
            return e.Message;
            
        }
    }


    public string GetTeacher(string tid)                                 //根据教师ID返回教师信息
    {
        try
        {
            SDBManage ByT = new SDBManage();
            int i;
            DataTable TeacherData = ByT.GetTeacher(tid);
            DataRowCollection Rows = TeacherData.Rows;
            IList<UserInfo> list = new List<UserInfo>();
            UserInfo temp = new UserInfo();

            for (i = 0; i < TeacherData.Rows.Count; i++)
            {
                DataRow currRow = Rows[i];
                temp.ID = (string)currRow["ID"];
                temp.Name = (string)currRow["Name"];
                temp.Sex = (string)currRow["Sex"];
                temp.Institute = (string)currRow["Institute"];
                temp.Department = (string)currRow["Department"]; ;
                temp.Class = "";
                list.Add(temp);
            }
            return JsonHelper.Serialize(temp);
        }
        catch(Exception e)
        {
            //return "{}";
            return e.Message;
        }
        
    }

    public string GetStudent(string sid)                                 //根据学生ID返回学生信息
    {
        try
        {
            SDBManage ByS = new SDBManage();
            int i;
            DataTable StudentData = ByS.GetStudent(sid);
            DataRowCollection Rows = StudentData.Rows;
            IList<UserInfo> list = new List<UserInfo>();
            UserInfo temp = new UserInfo();

            for (i = 0; i < StudentData.Rows.Count; i++)
            {
                DataRow currRow = Rows[i];
                temp.ID = (string)currRow["ID"];
                temp.Name = (string)currRow["Name"];
                temp.Sex = (string)currRow["Sex"];
                temp.Institute = (string)currRow["Institute"];
                temp.Department = (string)currRow["Department"];
                temp.Class = (string)currRow["Class"];
                list.Add(temp);
            }
            return JsonHelper.Serialize(temp);
        }
        catch(Exception e)
        {
            //return "{}";
            return e.Message;
        }
    }

    [WebMethod]
    public string GetUser(string id, string pwd, int role)               //返回验证信息
    {
        SDBManage manager = new SDBManage();
        bool Check = manager.CheckUser(id, pwd, role);
        //bool IsFirst = SDBManager.IsFirst(id, pwd, role);
        if (Check == true)
        {
            if (role == 2)                                                 //学生
            {
                return GetStudent(id);
            }
            else                                                           //教师
            {
                return GetTeacher(id);
            }
        }
        else
        {
            //return "{}";
            return "wrong role";
        }
    }

    //[WebMethod]
    //public bool IsFirst(string id, string pwd, int role)                 //返回是否第一次登入
    //{
    //    SDBManage SDBManager = new SDBManage();
    //    bool IsFirst = SDBManager.IsFirst(id, pwd, role);
    //    return IsFirst;
    //}
}

